import { defineStore } from 'pinia';
import router from '@/router/index.js';
// 引入公共组件layout
import Layout from '@/components/layout/index.vue';

export const useRouteStore = defineStore('useRouteStore ', {
  state: () => ({
    addRoutes: [],
    menuList: [],
    allRoutes: [],
  }),
  actions: {
    // 路由生成
    setDynamicRoutes (router, asyncRouter) {
      // 添加动态路由
      this.addRoutes = asyncRouter;
      // 生成所有路由（无权限路由和动态路由）
      this.allRoutes = router.concat(asyncRouter);
    },

    // 菜单生成
    setMenuList (menus) {
      this.menuList = menus;
    },

    // 根据角色权限过滤路由
    roleRouterFilter (rpArr) {
      const res = [];
      rpArr.forEach((pItem) => {
        if (pItem.meta.requiresAuth) {
          if (pItem.children && pItem.children.length > 0) {
            pItem.children = this.roleRouterFilter(pItem.children);
          }
          res.push(pItem);
        }
      });
      return res;
    },

    // 获取动态路由
    aysncRouterFn () {
      const aysncRouter = [
        {
          path: '/echarts',
          component: Layout,
          meta: { title: 'echarts组件', requiresAuth: true, hidden: false },
          children: [
            {
              path: '/sectorArea',
              name: 'sectorArea',
              component: () => import('@/views/echartsModule/sectorArea.vue'),
              meta: { title: '扇形区域放大', requiresAuth: true, hidden: false },
            },
            {
              path: '/roundArea',
              name: 'roundArea',
              component: () => import('@/views/echartsModule/roundArea.vue'),
              meta: { title: '圆形区域放大', requiresAuth: true, hidden: false },
            },
            {
              path: '/lineText',
              name: 'lineText',
              component: () => import('@/views/echartsModule/lineText.vue'),
              meta: { title: '折线图添加文本', requiresAuth: true, hidden: false },
            },
            // 其他系统管理路由...
          ],
        },
        {
          path: '/functionalComponents',
          component: Layout,
          meta: { title: '功能组件', requiresAuth: true, hidden: false },
          children: [
            {
              path: '/filePreview',
              name: 'filePreview',
              component: () => import('@/views/functionalComponents/filePreview.vue'),
              meta: { title: '文件预览', requiresAuth: true, hidden: false },
            },
            {
              path: '/pageExportImg',
              name: 'pageExportImg',
              component: () => import('@/views/functionalComponents/pageExportImg.vue'),
              meta: { title: '页面导出图片', requiresAuth: true, hidden: false },
            },
            {
              path: '/virtualScroll',
              name: 'virtualScroll',
              component: () => import('@/views/functionalComponents/virtualScroll.vue'),
              meta: { title: '虚拟滚动', requiresAuth: true, hidden: false },
            },
          ],
        },
        {
          path: '/',
          component: Layout,
          redirect: '/gridLayout',
          meta: { title: 'grid布局', requiresAuth: true, hidden: false },
          children: [
            {
              path: '/gridLayout',
              name: 'gridLayout',
              component: () => import('@/views/gridLayout/gridLayoutIndex.vue'),
              meta: { title: 'grid布局', requiresAuth: true, hidden: false },
            },
          ],
        },

        {
          path: '/system',
          component: Layout,
          meta: { title: '系统管理', requiresAuth: true, hidden: false },
          children: [
            {
              path: '/role',
              name: 'role',
              component: () => import('@/views/system/role/roleIndex.vue'),
              meta: { title: '角色管理', requiresAuth: true, hidden: false },
            },
            {
              path: '/role1',
              name: 'role1',
              component: () => import('@/views/system/role1/roleIndex.vue'),
              meta: { title: '角色管理1', requiresAuth: true, hidden: false },
            },
            // 其他系统管理路由...
          ],
        },
      ];
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(aysncRouter);
        }, 1000);
      });
    },

    // 添加动态路由到router组件
    async addRoutesAct (routeArr) {
      this.addRoutes.forEach((routeItem) => {
        if (!router.hasRoute(routeItem.name)) {
          router.addRoute(routeItem);
        }
      });
    },
  },
});
